﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using static Proxy.Globle;

namespace Proxy
{
    public class GantryTransport
    {
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        private CommonHandle Handle_Common;
        CanvasExample canvas;
        public static ActUtlType64Lib.ActUtlType64Class lpcom_ReferencesUtlType = new ActUtlType64Lib.ActUtlType64Class();

        public GantryTransport()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Common = new CommonHandle();
            InitMitsubishi();
            Task.Factory.StartNew(() => Task_Transport());
            //Task.Factory.StartNew(() => Task_Movement());
            //Task.Factory.StartNew(() => Task_PreStack());
        }

        public void InitMitsubishi()
        {
            lpcom_ReferencesUtlType.ActLogicalStationNumber = 1;
            lpcom_ReferencesUtlType.ActPassword = "";
            int returnCode = lpcom_ReferencesUtlType.Open();
            if (returnCode == 0)
            {
                Globle.isMitsubishiActive = true;
            }
        }

        /// <summary>
        ///
        /// </summary>
        private void Task_Transport()
        {
            do
            {
            Start:
                Thread.Sleep(500);
                try
                {
                    if (Globle.isInfeedActive || Globle.isOutfeedActive)
                    {


                        goto Start;
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_Infeed.Error(ex.Message);
                    goto Start;
                }
            } while (true);
        }

        /// <summary>
        /// 移库功能
        /// </summary>
        private void Task_Movement()
        {
            do
            {
            Start:
                Thread.Sleep(1000 * 60 * 5);
                try
                {
                    if (Globle.isMovementActive)
                    {
                        lpcom_ReferencesUtlType.GetDevice("D3300", out int gantryState);

                        if (!gantryState.Equals((int)EnumOfGantryState.空闲中))
                        {
                            goto Start;
                        }

                        //获取正在执行JOB
                        var jobInfos = Handle_Search.GetJobList();

                        if (jobInfos != null)
                        {
                            goto Start;
                        }

                        //整理库存，检测是否需要生成移库的任务
                        var Organize_Needed = false;
                        //需要 下达任务
                        if (Organize_Needed)
                        {
                        }
                        else
                        {
                            goto Start;
                        }
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_Outfeed.Error(ex.Message);
                    goto Start;
                }
            } while (true);
        }

        /// <summary>
        /// 预叠板
        /// </summary>
        public void Task_PreStack()
        {
            do
            {
            Start:
                Thread.Sleep(1000 * 60 * 5);
                try
                {
                    goto Start;
                }
                catch (Exception ex)
                {
                    goto Start;
                }
            }
            while (true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="interfaceStruct"></param>
        void SendMsg2KLF(InterfaceStruct interfaceStruct)
        {
            try
            {
                int value = (int)-10; // 将负数转换为无符号整数
                lpcom_ReferencesUtlType.WriteDeviceBlock("D3200", 1, ref value);
                //下发任务
                //X_Pick
                lpcom_ReferencesUtlType.SetDevice("D3200", interfaceStruct.D3200);
                if (interfaceStruct.D3200 > 0)
                {
                    lpcom_ReferencesUtlType.SetDevice("D3201", 0);
                }
                else
                {
                    lpcom_ReferencesUtlType.SetDevice("D3201", -1);
                }
                //Y_Pick
                lpcom_ReferencesUtlType.SetDevice("D3202", interfaceStruct.D3202);
                //Z_Pick
                lpcom_ReferencesUtlType.SetDevice("D3204", interfaceStruct.D3204);
                //R_Pick
                lpcom_ReferencesUtlType.SetDevice("D3206", interfaceStruct.D3206);
                //X_Put
                lpcom_ReferencesUtlType.SetDevice("D3208", interfaceStruct.D3208);
                if (interfaceStruct.D3208 > 0)
                {
                    lpcom_ReferencesUtlType.SetDevice("D3209", 0);
                }
                else
                {
                    lpcom_ReferencesUtlType.SetDevice("D3209", -1);
                }
                //Y_Put
                lpcom_ReferencesUtlType.SetDevice("D3210", interfaceStruct.D3210);
                //Z_Put
                lpcom_ReferencesUtlType.SetDevice("D3212", interfaceStruct.D3212);
                //R_Put
                lpcom_ReferencesUtlType.SetDevice("D3214", interfaceStruct.D3214);
                //start
                lpcom_ReferencesUtlType.SetDevice("M2200", 1);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        struct InterfaceStruct
        {
            public int D3200 { get; set; }
            public int D3202 { get; set; }
            public int D3204 { get; set; }
            public int D3206 { get; set; }
            public int D3208 { get; set; }
            public int D3210 { get; set; }
            public int D3212 { get; set; }
            public int D3214 { get; set; }
            public int D3400 { get; set; }
        }
    }
}